<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%!
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
	int bokeshul = 0;
	Map<String, String> hashMapm = new HashMap<String, String>();
	Map<String, String> hashMapn = new HashMap<String, String>();
%>
    <%!
        String namedid = "0";
    %>
    <% 
        String strSessionj = (String)request.getSession().getAttribute("namet");  
        System.out.println("JSP:" + strSessionj);
        namedid = strSessionj;
    %>
<title><%=strSessionj%>的博客</title>
<style type="text/css">
body {
	text-align: center;
}

a {
	text-decoration: none;
	color: black;
}
a:hover {
    color: red;
}
.idsize{
    width: 70px;
    height: 70px;
}
.bkksize{
    width: 700px;
}
table{
    position: relative;
	left: 70px;
	top: 110px;
}
.div{
    display: none;
}
.djcx1{
    position: absolute;
    left: 10px;
    top: 10px;
}
.djcx2{
    position: absolute;
    left: 270px;
    top: 10px;
}
.djcx3{
    position: absolute;
    left: 100px;
    top: 10px;
}
.djcx4{
    position: absolute;
    left: 430px;
    top: 10px;
}
</style>
</head>
<%
	try {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
		e.printStackTrace();
	}
	Connection conns;
	try {
		conns = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
		Statement stmts = conns.createStatement();
		String sqll = "select id from user_zc where name = '" + namedid + "'";
		ResultSet rsl = stmts.executeQuery(sqll);
		int setnamedis = 0;
		while (rsl.next()) {
			int getnamedis = rsl.getInt(1);
			setnamedis = getnamedis;
		}
		System.out.println("user_zc id:" + setnamedis);
		System.out.println("***********************************");
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		String sqlcountbbk = "select count(author_id) total from blog";
		ResultSet rscountbbk = stmts.executeQuery(sqlcountbbk);
		int setcountbbk = 0;
		while (rscountbbk.next()) {
			int getcountbbk = rscountbbk.getInt(1);
			setcountbbk = getcountbbk;
		}
		System.out.println("setcountbbk:" + setcountbbk);
		String sqlcountbbks = "select author_id = " + setnamedis + " from blog";
		ResultSet rscountbbks = stmts.executeQuery(sqlcountbbks);
		int bbks = 1;
		Map<String, String> hashMapbbks = new HashMap<String, String>();
		while (rscountbbks.next()) {
			hashMapbbks.put("author_id" + bbks, rscountbbks.getString(1));
			System.out.println("author_id" + bbks + ":" + hashMapbbks.get("author_id" + bbks));
			bbks ++;
		}
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
		int bbkkss = 1;
		boolean shifouabc = true;
		while(bbkkss < bbks){
			System.out.println("author_id" + bbkkss + ":" + hashMapbbks.get("author_id" + bbkkss));
			if(!hashMapbbks.get("author_id" + bbkkss).equals("0")){
				shifouabc = false;
			}
			bbkkss ++;
		}
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
		System.out.println("shifouabc:" + shifouabc);
		if(shifouabc){
			response.sendRedirect("/wumeiyoubbk.html");return;
		}
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		System.out.println("***********************************");
		String sqlm = "select * from blog where author_id = " + setnamedis;
		ResultSet rsm = stmts.executeQuery(sqlm);
		int i = 1;
		while (rsm.next()) {
			hashMapm.put("id" + i, rsm.getString("id"));
     		System.out.println("blog id:" + hashMapm.get("id" + i));
			hashMapn.put("title" + i, rsm.getString("title"));
     		System.out.println("title:" + hashMapn.get("title" + i));
		    System.out.println("***********************************");
		    i ++;
		}
		System.out.println("i:" + i);
		int bokenum = i - 1;
		bokeshul = bokenum;
		System.out.println("bokenum:" + bokenum);
		String firstdid = hashMapm.get("id1");
		System.out.println("firstdid:" + firstdid);
		int zhfirstdid = Integer.parseInt(firstdid);
		System.out.println("zhfirstdid:" + zhfirstdid);
		response.getWriter().append("<table border='1' id='cs'>");
		response.getWriter().append("<tr>");
		response.getWriter().append("<th class='idsize'>ID</th>");
		response.getWriter().append("<th class='bkksize'>博客</th>");
		response.getWriter().append("</tr>");
		response.getWriter().append("</table>");
		int k = 1;
		while(k <= bokenum) {
			response.getWriter().append("<table border='1' id='" + hashMapm.get("id" + k) + "'>");
			response.getWriter().append("<tr>");
			response.getWriter().append("<th class='idsize'>");
			response.getWriter().append("<a onclick='wdcsid()'>");
			response.getWriter().append(hashMapm.get("id" + k));
			response.getWriter().append("</a>");
			response.getWriter().append("</th>");
			response.getWriter().append("<th class='bkksize'>");
			response.getWriter().append("<a onclick='wdcsid()'>");
			response.getWriter().append(hashMapn.get("title" + k));
			response.getWriter().append("</a>");
			response.getWriter().append("</th>");
			response.getWriter().append("</tr>");
			response.getWriter().append("</table>");
			k ++;
			zhfirstdid ++;
		}
		rsm.close();
		rscountbbks.close();
		rscountbbk.close();
		rsl.close();
		stmts.close();
		conns.close();
	} catch (SQLException e) {
		e.printStackTrace();
	}
%>
<body onmousemove="pos()">
    <!--  
	<span class="div djcx2">鼠标当前在:</span>
	<span id="show" class="div djcx4"></span>
	-->
	<form method="post" name="pasorms" action="blogpage.jsp">
		<input type='hidden' name="csflid">
		<input type='hidden' name="csflids">
	</form>
	<input class="btn djcx1" type="button" value="删除博客"/>
	<form method="post" name="getdeleid">
		<input type='hidden' name="setdeleid">
	</form>
        <%
	    System.out.println("###################");
        int n = 1;
        response.getWriter().append("<select class='div djcx3'>");
        response.getWriter().append("<option>选择要删除的博客ID</option>");
	    while(n <= bokeshul) {
	    	String idsrt = hashMapm.get("id" + n);
     		System.out.println("blog id string:" + idsrt);
            response.getWriter().append("<option>");
            response.getWriter().append(idsrt);
            response.getWriter().append("</option>");
	    	n ++;
	    }
        response.getWriter().append("</select>");
	    System.out.println("###################");
	    String txtoptions = request.getParameter("setdeleid"); 
	    //out.println(txtoptions);
	    System.out.println(txtoptions);
	    if(txtoptions != null){
	    	try {
			    Class.forName("com.mysql.jdbc.Driver").newInstance();
		    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			    e.printStackTrace();
		    }
		    Connection connst = null;
		    PreparedStatement pst = null;
		    try {
			    connst = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			    Statement stmnt = connst.createStatement();
			    System.out.println("***********************************");
			    String sqlq = "delete from blog where id = ?";
				pst = connst.prepareStatement(sqlq);
				pst.setString(1, txtoptions);
				pst.executeUpdate();
				String sqlshul = "select count(id) total from review where bei_id = " + txtoptions;
				ResultSet rsshul = stmnt.executeQuery(sqlshul);
				int setpinglshul = 0;
				while (rsshul.next()) {
					int getpinglshul = rsshul.getInt(1);
					setpinglshul = getpinglshul;
				}
				System.out.println("the count of pl:" + setpinglshul);
				String scsuoypingl = "delete from review where bei_id = " + txtoptions + " order by id limit ?";
				pst = connst.prepareStatement(scsuoypingl);
				pst.setInt(1, setpinglshul);
				pst.executeUpdate();
			    System.out.println("***********************************");
				rsshul.close();
				stmnt.close();
			    pst.close();
			    connst.close();
	            response.sendRedirect("/myblog.jsp");
		    } catch (SQLException e) {
			    e.printStackTrace();
		    }
	    }
        %>
	<!--  
	<form action="linshi.jsp" method="post">
    <span class="div djcx3">输入要删除的博客的ID</span><input class="div djcx2" type="text" name="deleteid"/>
    <button class="div djcx4" type="submit">提交</button>
    </form>
    -->
    <br><br><br><br><br><br>
    <h1></h1>
	<script type="text/javascript" src="/static/js/jquery.min.js"></script>
	<script type="text/javascript">
	var diffiernumt = 0;
	var optionzhi = "0";
	function fnGetTable(oEl) {
		try {
			while (null != oEl && oEl.tagName != "TABLE") {
				oEl = oEl.parentElement;
			}
			return oEl;
		} catch (e) {
			return null;
		}
	}
	function pos() {
		try {
			oElement = document.elementFromPoint(event.x, event.y);
			oElement = fnGetTable(oElement);
			if (oElement.tagName == "TABLE")
				//show.innerText = oElement.id;
			    diffiernumt = oElement.id;
		} catch (e) {
		}
	}
	function setinputmn(csflid,csflids){
		document.pasorms.csflid.value = csflid;
		document.pasorms.csflids.value = csflids;
	    document.pasorms.submit();
	}
	function wdcsid(){
		setinputmn(diffiernumt,"mdzz");
	}
	$(document).ready(function(){
	    $(".btn").click(function(){
	        $(".div").toggle();
	        })
	});
	$("option").click(function(event) {  
		  var $target = $(event.target);  
		  //$('#show').html($target.text());
		  //alert("target.text():" + $target.text());
		  var nazhi = $target.text();
		  //alert("nazhi:" + nazhi);
	      optionzhi = nazhi;
		  //alert("optionzhi option:" + optionzhi);
		  chufaz();
	});
	function setdeleidmn(setdeleid){
		document.getdeleid.setdeleid.value = setdeleid;
	    document.getdeleid.submit();
	}
	function chufaz(){
		//alert("optionzhi onclick chufa():" + optionzhi);
		setdeleidmn(optionzhi);
		alert("博客删除成功");
	}
	</script>
</body>
</html>